package ch01.demo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class demo7 {
    public static void main(String[] args) {
        //测试条件反射
        try{
            Class clz=Class.forName("ch01.demo.Person");
            Object obj=clz.newInstance();
            System.out.println(obj);

            //获取Person的无参构造
            Constructor c1=clz.getDeclaredConstructor();
            //Person的无参构造为public,这里可以直接访问
            obj =c1.newInstance();
            System.out.println(obj);

            //获取Person的单参数构造
            Constructor c2 = clz.getDeclaredConstructor(String.class);
            //Person的单参构造为private，这里已超出其访问范围，不能直接访问
            //通过setAccessible方法，设定为可访问
            c2.setAccessible(true);
            obj=c2.newInstance("New Person");   //new Person ("New Person")
            System.out.println(obj);

            //获取Person的三参构造
            Constructor c3=clz.getDeclaredConstructor(String.class,String.class,String.class);
            //Person的三参构造为protected,这里已超出其访问范围，不能直接访问
            //通过setAccessable方法，设定为可以访问
            c3.setAccessible(true);
            obj=c3.newInstance("New Person","beijing","Hello!");
            System.out.println(obj);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }catch (SecurityException e){
            e.printStackTrace();
        }catch (NoSuchMethodException e){
            e.printStackTrace();
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }catch (InstantiationException e){
            e.printStackTrace();
        } catch (InvocationTargetException e){
            e.printStackTrace();
        }
    }
}
